package com.wechart.controller;

import com.wechart.dao.domain.*;
import com.wechart.model.ReportModel;
import com.wechart.service.manage.OrginfoService;
import com.wechart.service.report.*;
import com.wechart.service.system.PrtryinfoService;
import com.wechart.utils.CommonOption;
import com.wechart.utils.CsvWriter;
import com.wechart.utils.DateUtil;
import com.wechart.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by silenceholic on 2017/5/2.
 */
@Controller
@RequestMapping("report/")
public class ReportController extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(ReportController.class);

    @Resource(name = "batchDetailService")
    private BatchDetailService batchDetailService;

    @Resource(name="olntranslistService")
    private OlntranslistService olntranslistService;

    @Resource(name="batchpmtinfoService")
    private BatchpmtinfoService batchpmtinfoService;

    @Resource(name = "translistService")
    private TranslistService translistService;

    @Resource(name = "prtryinfoService")
    private PrtryinfoService prtryinfoService;

    @Resource(name = "packageinfoService")
    private PackageinfoService packageinfoService;

    @Resource(name = "orginfoService")
    private OrginfoService orginfoService;


    /**
     * 在线支付交易流水
     * @return
     */
    @RequestMapping(value = "olntranslist.htm")
    public String allOlntranslist(){
        return "report/olntranslist";
    }
    @ResponseBody
    @RequestMapping(value = "olntranslist.do" )
    public Object getAllOlntranslist(@ModelAttribute ReportModel reportModel){
        Map<String, Object> map = new HashMap<String, Object>();
//        List<Olntranslist> data = olntranslistService.getOlntranslist(reportModel);
        List<Olntranslist> data=olntranslistService.selectOlntranslistByPage(reportModel);
        map.put("begin",reportModel.getBegin());
        map.put("end",reportModel.getEnd());
        map.put("code",reportModel.getCode());
        map.put("data",data);
        int count = olntranslistService.countOlntranslist(reportModel);
        map.put("recordsTotal", count);
        map.put("recordsFiltered", count);
//        ModelAndView mv = super.modelAndView(map);
//        mv.setViewName("report/olntranslist");
        return map;
    }
    @RequestMapping(value = "batchpmtinfo.htm")
    public String allBatchpmtinfo(){
        return "report/batchpmtinfo";
    }
    @ResponseBody
    @RequestMapping(value = "batchpmtinfo.do" )
    public Object getBatchpmtinfo(@ModelAttribute ReportModel reportModel){
        Map<String, Object> map = new HashMap<String, Object>();
//        List<Batchpmtinfo> data = batchpmtinfoService.getBatchpmtinfo(reportModel);
        List<Batchpmtinfo> data=batchpmtinfoService.selectBatchpmtinfoByPage(reportModel);
        map.put("data",data);
        map.put("begin",reportModel.getBegin());
        map.put("end",reportModel.getEnd());
        map.put("code",reportModel.getCode());
        int count = batchpmtinfoService.countBatchpmtinfo(reportModel);
        map.put("recordsTotal", count);
        map.put("recordsFiltered", count);
//        ModelAndView mv = super.modelAndView(map);
//        mv.setViewName("report/batchpmtinfo");
        return map;
    }


    /**
     * 批量报文跳转
     * @return
     */
    @RequestMapping(value = "batchDetail.htm")
    public String allBatchDetailTables(){
        return "report/batchdetail";
    }
    /**
     * 批量报文查询
     * @param reportModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "batchDetail.do" )
    public Object getAllBatchDetailTables(@ModelAttribute ReportModel reportModel){

        Map<String, Object> map = new HashMap<String, Object>();
//        List<Batchdetail> data = batchDetailService.getAllBatchDetail(reportModel);
        List<Batchdetail> data=batchDetailService.selectBatchdetailByPage(reportModel);
        map.put("data",data);
        int count = batchDetailService.countBatchDetail(reportModel);
        map.put("recordsTotal", count);
        map.put("recordsFiltered", count);
        return map;
    }

    /**
     * 报文信息表
     * @return
     */
    @RequestMapping(value = "packageinfo.htm")
    public String allPackageinfoTables(){
        return "report/packageinfo";
    }
    /**
     * 报文信息表
     * @param reportModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "packageinfo.do" )
    public Object getAllPackageinfoTables(@ModelAttribute ReportModel reportModel){
        Map<String, Object> map = new HashMap<String, Object>();
//        List<Packageinf> data = packageinfoService.getAllPackageinfo(reportModel);
        List<Packageinf> data=packageinfoService.selectPackageinfoByPage(reportModel);
        map.put("data",data);
        int count = packageinfoService.countPackageinfo(reportModel);
        map.put("recordsTotal", count);
        map.put("recordsFiltered", count);
        return map;
    }


    /**
     * 批量报文下载
     * @param reportModel
     * @return
     */
//    @ResponseBody
    @RequestMapping(value = "downloadBatchDetail.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadBatchDetailTables(@ModelAttribute ReportModel reportModel)
    throws Exception{
        String end = DateUtil.getToday();
        String start =DateUtil.getYesterday();

        if (StringUtils.isNoNullString(reportModel.getStart())) {
            start = reportModel.getStart().replaceAll("-","");
        }

        if (StringUtils.isNoNullString(reportModel.getEnd())) {
            end = reportModel.getEnd().replaceAll("-","");
        }
        List<Batchdetail> data = batchDetailService.getAllBatchDetail(reportModel);
//        List<DayCrawlerMailAgg> list = crawlerService.getDayCrawlerMailAgg(start_date, end_date, URLDecoder.decode(ruledesc, "UTF-8"));
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }

    /**
     * 批量业务收付款信息下载
     * @param reportModel
     * @return
     */
    @RequestMapping(value = "downloadBatchpmtinfo.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadBatchpmtinfoTables(@ModelAttribute ReportModel reportModel)
            throws Exception{

        List<Batchpmtinfo> data = batchpmtinfoService.getBatchpmtinfo(reportModel);
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }

    /**
     * 在线支付交易流水下载
     * @param reportModel
     * @return
     */
    @RequestMapping(value = "downloadOlntranslist.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadOlntranslist(@ModelAttribute ReportModel reportModel)
            throws Exception{

        List<Olntranslist> data = olntranslistService.getOlntranslist(reportModel);
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }
    /**
     * 交易流水第一次请求
     * @return
     */
    @RequestMapping(value = "translist.htm")
    public String allTranslist(){
        return "report/translist";

    }
    /**
     * 交易流水二次以上的请求
     * @param reportModel
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "translist.do" )
    public Object getAllTranslistTables(@ModelAttribute ReportModel reportModel){

        Map<String, Object> map = new HashMap<String, Object>();
        String end = DateUtil.getToday();
        String start =DateUtil.getYesterday();
        if (StringUtils.isNoNullString(reportModel.getStart())){
            start = reportModel.getStart().replaceAll("-","");
        }
        if (StringUtils.isNoNullString(reportModel.getEnd())){
            end = reportModel.getEnd().replaceAll("-","");
        }
//        List<Translist> data = translistService.getAllTranslist(reportModel);
        List<Translist> data=translistService.selectTranslistByPage(reportModel);
        System.out.println("取出流水数量："+data.size());

        int count = translistService.countTranslist(reportModel);
        map.put("data",data);
        map.put("recordsTotal", count);
        map.put("recordsFiltered", count);
        map.put("start",reportModel.getStart());
        map.put("end",reportModel.getEnd());
        map.put("code",reportModel.getCode());
//        ModelAndView mv = super.modelAndView(map);
//        mv.setViewName("report/translist");
        return map;
    }
    /**
     * 取得业务类型
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "select/selectPrtry.do")
    public Object selectPrtry(@RequestParam(value = "q",required = false) String  name){
        return prtryinfoService.selectPrtry(name);
    }

    /**
     * 交易流水信息下载
     * @param reportModel
     * @return
     */
    @RequestMapping(value = "downloadTranslist.do" , method = { RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<byte[]> downloadTranslistTables(@ModelAttribute ReportModel reportModel)
            throws Exception{

        List<Translist> data = translistService.getAllTranslist(reportModel);
        HttpHeaders headers = new HttpHeaders();
        String file = DateUtil.formate(new Date(), "FULL");
        String fileName = new String((file + ".csv").getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题
        headers.setContentDispositionFormData("attachment", fileName);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(CsvWriter.print(data), headers, HttpStatus.CREATED);

    }
    /**
     * 取得机构号和机构名称
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "select/orgIdAndName.do")
    public Object selectOrgidAndName(@RequestParam(value = "q",required = false) String  name){
        return orginfoService.selectOrgidAndName(name);
    }

    /**
     * 跳转到显示机构信息详细界面
     */
    @ResponseBody
    @RequestMapping(value = "showOrginfo.do")
    public Object showOrginfo(@RequestParam(value = "sender",required = false) String  sender){
        Orginfo orginfo=orginfoService.getOrginfoByPK(sender);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orgtypes", CommonOption.getOrgtypeOption());
        map.put("busiallows",CommonOption.getBusiallowOption());
        map.put("consigntypes",CommonOption.getConsigntypeOption());
        map.put("checkflags",CommonOption.getCheckflagOption());
        map.put("mactypes",CommonOption.getMactypeOption());
        map.put("nodetypes",CommonOption.getNodetypeOption());
        map.put("svctypes",CommonOption.getSvctypeOption());
        map.put("commtypes",CommonOption.getCommtypeOption());
        map.put("orgstats",CommonOption.getOrgstatOption());
        map.put("downmodes",CommonOption.getDownmodeOption());
        map.put("item",orginfo);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("report/showorginfo");
        return mv;
    }

    /**
     * 跳转到显示流水表详细信息界面
     */
    @ResponseBody
    @RequestMapping(value = "showTranslistDetail.do")
    public Object showTranslistDetail(@RequestParam(value = "refid",required = false) String refid){
        Translist translist=translistService.getTranslistByRefid(refid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("item",translist);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("report/showTranslistDetail");
        return mv;
    }
    /**
     * 跳转到显示在线流水表详细信息界面
     */
    @ResponseBody
    @RequestMapping(value = "showOlntranslistDetail.do")
    public Object showOlntranslistDetail(@RequestParam(value = "refid",required = false) String refid){
        Olntranslist olntranslist=olntranslistService.getOlntranslistByRefid(refid);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("item",olntranslist);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("report/showOlntranslistDetail");
        return mv;
    }
    /**
     * 跳转到显示批量业务明细详细界面
     */
    @ResponseBody
    @RequestMapping(value = "showBatchDetail.do")
    public Object showBatchDetail(@RequestParam(value = "sendbatchno",required = false) String sendbatchno){
        Batchdetail batchdetail=batchDetailService.getBatchdetailBySendBatchNo(sendbatchno);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("item",batchdetail);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("report/showBatchDetail");
        return mv;
    }
    /**
     * 跳转到显示批量业务明细详细界面
     */
    @ResponseBody
    @RequestMapping(value = "showAppendBatNo.do")
    public Object showAppendBatNo(@RequestParam(value = "appendbatno",required = false) String appendbatno){
        Batchpmtinfo batchpmtinfo=batchpmtinfoService.getBatchpmtinfoByAppendBatNo(appendbatno);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("item",batchpmtinfo);
        ModelAndView mv = super.modelAndView(map);
        mv.setViewName("report/showBatchpmtinfoDetail");
        return mv;
    }




}
